package com.good.cm.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.good.cm.dao.TriagePatientMapper;
import com.good.cm.exception.ParamsException;
import com.good.cm.model.ResultInfo;
import com.good.cm.utils.RandomGUID;
import com.good.cm.vo.TriagePatient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PatientService {
    @Resource
    private TriagePatientMapper patientMapper;

    /**
     * 添加患者信息
     * @param triagePatient
     * @return
     */
    public ResultInfo addPatient(TriagePatient triagePatient){
        ResultInfo info=new ResultInfo();
        System.out.println("患者信息:"+ JSON.toJSONString(triagePatient));
        if(triagePatient ==null)
        {
            throw new ParamsException("患者为空");
        }
        if(StringUtils.isBlank(triagePatient.pvid))
        {
            triagePatient.pvid= RandomGUID.generatorGUID();
            triagePatient.status=2;
            triagePatient.visitdate=new Date(System.currentTimeMillis());
            System.out.println("主键："+ triagePatient.pvid);
            int rc= patientMapper.insert(triagePatient);
            if (rc<1)
            {
                info.setCode(500);
                info.setMsg("新增失败");
            }
        }else
         {
            UpdateWrapper<TriagePatient> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("pvid", triagePatient.pvid);
            int rc=   patientMapper.update(triagePatient, updateWrapper);
            if (rc<1)
            {
                info.setCode(500);
                info.setMsg("更新失败");
            }
        }
        return  info;
    }
    /**
     * 通过pvid主键获取患者信息
    * */
    public ResultInfo getPatient(String pvid){
        System.out.println("患者id:"+pvid);
        ResultInfo info=new ResultInfo();
        QueryWrapper<TriagePatient> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pvid",pvid);
        List<TriagePatient> patList= patientMapper.selectList(queryWrapper);
        if (patList!=null && patList.size()>0)
        {
            info.setResult(patList.get(0));
        }else {
            info.setCode(500);
            info.setMsg("通过pvid没有查询到患者信息");
        }
        return  info;
    }

    /**
     * 删除患者
     * @param
     * @return
     */
    public ResultInfo deletePatient(TriagePatient triagePatient)
    {
        System.out.println("患者id:"+ triagePatient.patientid);
        ResultInfo info=new ResultInfo();
        UpdateWrapper<TriagePatient> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pvid", triagePatient.pvid);
        updateWrapper.set("status", 0);
        patientMapper.update(null,updateWrapper);
        return  info;
    }
}
